package com.instasale.models;

import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;


@Entity
@Table(name = "CARS")
public class Car implements Serializable {

    public static final String COLUMN_DESCRIPTION = "DESCRIPTION";
    public static final String COLUMN_MODEL = "MODEL";
    public static final String COLUMN_PRICE = "PRICE";
    public static final String COLUMN_ENGINE_TYPE = "ENGINE_TYPE";
    public static final String COLUMN_USED = "USED";
    public static final String COLUMN_SELLER_ID = "SELLER_ID";
    public static final String COLUMN_AVAILABLE_DATE = "AVAILABLE_DATE";
    public static final String COLUMN_MILEAGE = "MILEAGE";

    @Id
    @Column(name = "ID")
    @GeneratedValue
    private Integer id;

    @Size(max = 200)
    @Column(name = COLUMN_DESCRIPTION)
    private String description;

    @NotNull(message = "Please enter model")
    @Size(min = 1, max = 30)
    @Column(name = COLUMN_MODEL)
    private String model;

    @NotNull(message = "Please enter price")
    @Min(0)
    @Column(name = COLUMN_PRICE)
    private Integer price = 0;

    @NotNull(message = "Please select engine type")
    @Column(name = COLUMN_ENGINE_TYPE)
    private EngineType engineType;

    @Column(name = COLUMN_USED)
    private Boolean used;

    @NotNull(message = "Please enter date from which this proposition is available")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @Column(name = COLUMN_AVAILABLE_DATE)
    private Date availableDate = new Date(System.currentTimeMillis());

    @NotNull(message = "Please select seller")
    @ManyToOne
    @JoinColumn(name = COLUMN_SELLER_ID)
    private Seller seller;

    @NotNull(message = "Please enter mileage")
    @Min(0)
    @Column(name = COLUMN_MILEAGE)
    private Integer mileage = 0;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
    @JoinTable(name = "CAR_FEATURE", joinColumns = {@JoinColumn(name = "CAR_ID")},
            inverseJoinColumns = {@JoinColumn(name = "FEATURE_ID")})
    private Set<Feature> features = new HashSet<Feature>(0);

    public Seller getSeller() {
        return seller;
    }

    public void setSeller(Seller seller) {
        this.seller = seller;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public EngineType getEngineType() {
        return engineType;
    }

    public void setEngineType(EngineType engineType) {
        this.engineType = engineType;
    }

    public Boolean getUsed() {
        return used;
    }

    public void setUsed(Boolean used) {
        this.used = used;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public Set<Feature> getFeatures() {
        return features;
    }

    public void setFeatures(Set<Feature> features) {
        this.features = features;
    }

    public Date getAvailableDate() {
        return availableDate;
    }

    public void setAvailableDate(Date available_date) {
        this.availableDate = available_date;
    }

    public Integer getMileage() {
        return mileage;
    }

    public void setMileage(Integer mileage) {
        this.mileage = mileage;
    }

    public static String[] getColumnNames() {
        String[] columns = {COLUMN_DESCRIPTION, COLUMN_ENGINE_TYPE,
                COLUMN_MODEL, COLUMN_PRICE, COLUMN_SELLER_ID, COLUMN_USED,
                COLUMN_MILEAGE, COLUMN_AVAILABLE_DATE};
        return columns;
    }
}
